/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.asu.cse360.service.client;

import edu.asu.cse360.model.Credentials;
import edu.asu.cse360.model.Employee;
import edu.asu.cse360.service.AuthenticationService;
import edu.asu.cse360.service.EmployeeService;


public class ClientAuthenticationService implements AuthenticationService{

    private EmployeeService employeeService;

    public ClientAuthenticationService(EmployeeService employeeService){
        this.employeeService = employeeService;
    }

    @Override
    public Employee authenticate(String username, String password) {
       return findEmployee(username, password);
    }

    private Employee findEmployee(String username, String password) {
       for(Employee emp : employeeService.getEmployees(null)){
           if(credentialsMatch(emp.getCredentials(), username, password)){
               return emp;
           }
       }
       return null;
    }

    private boolean credentialsMatch(Credentials creds, String username, String password) {
        return creds.getUsername().equals(username) && creds.getPassword().equals(password);
    }

}
